<?php
#################
# This is is the betting input page
# here the data is read into the horsesdb for each bet
#bet amount and charity (HOUSE TAKE) are set on this page
#keep these two figures as close as possbile to round figures 
#e.g.  try not to use 25 and .3, but 25 and .2 would work
#for adding horse names per race please load horseadd.html
#$horsefile = "d:/www/horses.txt"; //<-edit with root path to log file
#set bet amount per bet
$betcost = 25 ;
#set the % charity (house)  take
$charity =0.2 ;
$ccost = $betcost *  $charity;
$cost = $betcost - $ccost    ;
$bets3 = 1; 
$b3 = 0 ;
################################
#main entry for horses into database and update of payout
 if(isSet($_POST['b1'])){
$h=1;
$db=sqlite_open("horse.db");
$bresult=sqlite_query($db,"SELECT bets from race Where horse_num = $h");
$bets3 = sqlite_fetch_single($bresult)  ;
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
$tc = sqlite_fetch_single($cresult) ;
$chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
$tc=$tc+$cost;
$cm = $cm+$ccost;
$bets3++;
#$po3=$tc/$bets3 ;
sqlite_query($db, "UPDATE race SET bets ='$bets3' WHERE horse_num = '$h'",$sqliteerror);
sqlite_query($db, "UPDATE race SET decimal_odds ='$cm' WHERE horse_num = '9'",$sqliteerror);
sqlite_query($db, "UPDATE race SET payout ='$tc' WHERE horse_num = '9'",$sqliteerror);
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
#$res = sqlite_fetch_single($result)  ;
foreach ($result as $entry) {
 $bts= $entry['bets'] ;
 $po1=@ ($tc/$bts)/10;
 $po2=round($po1) ;
 $po=$po2*10;
$hn=$entry['horse_num'] ;
 sqlite_query($db, "UPDATE race SET payout ='$po' WHERE horse_num = '$hn'",$sqliteerror);
}
sqlite_close($db);
}
#function main()   {
#################
# form was submitted
if(isSet($_POST['b3'])) {
$h=3;
$db=sqlite_open("horse.db");
$bresult=sqlite_query($db,"SELECT bets from race Where horse_num = $h");
$bets3 = sqlite_fetch_single($bresult)  ;
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
$tc = sqlite_fetch_single($cresult) ;
$chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
$tc=$tc+$cost;
$cm = $cm+$ccost;
$bets3++;
#$po3=$tc/$bets3 ;
sqlite_query($db, "UPDATE race SET bets ='$bets3' WHERE horse_num = '$h'",$sqliteerror);
sqlite_query($db, "UPDATE race SET decimal_odds ='$cm' WHERE horse_num = '9'",$sqliteerror);
sqlite_query($db, "UPDATE race SET payout ='$tc' WHERE horse_num = '9'",$sqliteerror);
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
#$res = sqlite_fetch_single($result)  ;
foreach ($result as $entry) {
 $bts= $entry['bets'] ;
 $po1=@ ($tc/$bts)/10;
 $po2=round($po1) ;
 $po=$po2*10;
$hn=$entry['horse_num'] ;
 sqlite_query($db, "UPDATE race SET payout ='$po' WHERE horse_num = '$hn'",$sqliteerror);
}
sqlite_close($db);  
 }

if(isSet($_POST['b2'])){
$h=2;
$db=sqlite_open("horse.db");
$bresult=sqlite_query($db,"SELECT bets from race Where horse_num = $h");
$bets3 = sqlite_fetch_single($bresult)  ;
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
$tc = sqlite_fetch_single($cresult) ;
$chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
$tc=$tc+$cost;
$cm = $cm+$ccost;
$bets3++;
#$po3=$tc/$bets3 ;
sqlite_query($db, "UPDATE race SET bets ='$bets3' WHERE horse_num = '$h'",$sqliteerror);
sqlite_query($db, "UPDATE race SET decimal_odds ='$cm' WHERE horse_num = '9'",$sqliteerror);
sqlite_query($db, "UPDATE race SET payout ='$tc' WHERE horse_num = '9'",$sqliteerror);
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
#$res = sqlite_fetch_single($result)  ;
foreach ($result as $entry) {
 $bts= $entry['bets'] ;
 $po1=@ ($tc/$bts)/10;
 $po2=round($po1) ;
 $po=$po2*10;
$hn=$entry['horse_num'] ;
 sqlite_query($db, "UPDATE race SET payout ='$po' WHERE horse_num = '$hn'",$sqliteerror);
}
sqlite_close($db);
}
if(isSet($_POST['b4'])){
$h=4;
$db=sqlite_open("horse.db");
$bresult=sqlite_query($db,"SELECT bets from race Where horse_num = $h");
$bets3 = sqlite_fetch_single($bresult)  ;
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
$tc = sqlite_fetch_single($cresult) ;
$chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
$tc=$tc+$cost;
$cm = $cm+$ccost;
$bets3++;
#$po3=$tc/$bets3 ;
sqlite_query($db, "UPDATE race SET bets ='$bets3' WHERE horse_num = '$h'",$sqliteerror);
sqlite_query($db, "UPDATE race SET decimal_odds ='$cm' WHERE horse_num = '9'",$sqliteerror);
sqlite_query($db, "UPDATE race SET payout ='$tc' WHERE horse_num = '9'",$sqliteerror);
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
#$res = sqlite_fetch_single($result)  ;
foreach ($result as $entry) {
 $bts= $entry['bets'] ;
$po1=@ ($tc/$bts)/10;
 $po2=round($po1) ;
 $po=$po2*10;
$hn=$entry['horse_num'] ;
 sqlite_query($db, "UPDATE race SET payout ='$po' WHERE horse_num = '$hn'",$sqliteerror);
}
sqlite_close($db);    
}
if(isSet($_POST['b5'])){
$h=5;
$db=sqlite_open("horse.db");
$bresult=sqlite_query($db,"SELECT bets from race Where horse_num = $h");
$bets3 = sqlite_fetch_single($bresult)  ;
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
$tc = sqlite_fetch_single($cresult) ;
$chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
$tc=$tc+$cost;
$cm = $cm+$ccost;
$bets3++;
#$po3=$tc/$bets3 ;
sqlite_query($db, "UPDATE race SET bets ='$bets3' WHERE horse_num = '$h'",$sqliteerror);
sqlite_query($db, "UPDATE race SET decimal_odds ='$cm' WHERE horse_num = '9'",$sqliteerror);
sqlite_query($db, "UPDATE race SET payout ='$tc' WHERE horse_num = '9'",$sqliteerror);
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
#$res = sqlite_fetch_single($result)  ;
foreach ($result as $entry) {
 $bts= $entry['bets'] ;
$po1=@ ($tc/$bts)/10;
 $po2=round($po1) ;
 $po=$po2*10;
$hn=$entry['horse_num'] ;
 sqlite_query($db, "UPDATE race SET payout ='$po' WHERE horse_num = '$hn'",$sqliteerror);
}
sqlite_close($db);    
}
 if(isSet($_POST['b6'])){
$h=6;
$db=sqlite_open("horse.db");
$bresult=sqlite_query($db,"SELECT bets from race Where horse_num = $h");
$bets3 = sqlite_fetch_single($bresult)  ;
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
$tc = sqlite_fetch_single($cresult) ;
$chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
$tc=$tc+$cost;
$cm = $cm+$ccost;
$bets3++;
#$po3=$tc/$bets3 ;
sqlite_query($db, "UPDATE race SET bets ='$bets3' WHERE horse_num = '$h'",$sqliteerror);
sqlite_query($db, "UPDATE race SET decimal_odds ='$cm' WHERE horse_num = '9'",$sqliteerror);
sqlite_query($db, "UPDATE race SET payout ='$tc' WHERE horse_num = '9'",$sqliteerror);
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
#$res = sqlite_fetch_single($result)  ;
foreach ($result as $entry) {
 $bts= $entry['bets'] ;
 $po1=@ ($tc/$bts)/10;
 $po2=round($po1) ;
 $po=$po2*10;
$hn=$entry['horse_num'] ;
 sqlite_query($db, "UPDATE race SET payout ='$po' WHERE horse_num = '$hn'",$sqliteerror);
}
sqlite_close($db);    
}
if(isSet($_POST['b7'])){
$h=7;
$db=sqlite_open("horse.db");
$bresult=sqlite_query($db,"SELECT bets from race Where horse_num = $h");
$bets3 = sqlite_fetch_single($bresult)  ;
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
$tc = sqlite_fetch_single($cresult) ;
$chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
$tc=$tc+$cost;
$cm = $cm+$ccost;
$bets3++;
#$po3=$tc/$bets3 ;
sqlite_query($db, "UPDATE race SET bets ='$bets3' WHERE horse_num = '$h'",$sqliteerror);
sqlite_query($db, "UPDATE race SET decimal_odds ='$cm' WHERE horse_num = '9'",$sqliteerror);
sqlite_query($db, "UPDATE race SET payout ='$tc' WHERE horse_num = '9'",$sqliteerror);
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
#$res = sqlite_fetch_single($result)  ;
foreach ($result as $entry) {
 $bts= $entry['bets'] ;
 $po1=@ ($tc/$bts)/10;
 $po2=round($po1) ;
 $po=$po2*10;
$hn=$entry['horse_num'] ;
 sqlite_query($db, "UPDATE race SET payout ='$po' WHERE horse_num = '$hn'",$sqliteerror);
}
sqlite_close($db);    
}
if(isSet($_POST['b8'])){
$h=8;
$db=sqlite_open("horse.db");
$bresult=sqlite_query($db,"SELECT bets from race Where horse_num = $h");
$bets3 = sqlite_fetch_single($bresult)  ;
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
$tc = sqlite_fetch_single($cresult) ;
$chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
$tc=$tc+$cost;
$cm = $cm+$ccost;
$bets3++;
#$po3=$tc/$bets3 ;
sqlite_query($db, "UPDATE race SET bets ='$bets3' WHERE horse_num = '$h'",$sqliteerror);
sqlite_query($db, "UPDATE race SET decimal_odds ='$cm' WHERE horse_num = '9'",$sqliteerror);
sqlite_query($db, "UPDATE race SET payout ='$tc' WHERE horse_num = '9'",$sqliteerror);
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
#$res = sqlite_fetch_single($result)  ;
foreach ($result as $entry) {
 $bts= $entry['bets'] ;
 $po1=@ ($tc/$bts)/10;
 $po2=round($po1) ;
 $po=$po2*10;
$hn=$entry['horse_num'] ;
 sqlite_query($db, "UPDATE race SET payout ='$po' WHERE horse_num = '$hn'",$sqliteerror);
}
sqlite_close($db);    
}
 ###################################
 #clear race data including all bets, total cash and payouts
 if(isSet($_POST['clear'])){
$db=sqlite_open("horse.db");
sqlite_query($db, "UPDATE race SET payout ='0'",$sqliteerror);
sqlite_query($db, "UPDATE race SET bets ='0'",$sqliteerror);

sqlite_close($db);    
}
#######################
#main page and data entry form

echo <<<endHTML
<html>
<head><title>main Betting input page</title></head>
<body>
<center> <FONT SIZE=5>
endHTML;
$db=sqlite_open("horse.db"); 
 #Get the data from the curent race in progress
$result = sqlite_array_query($db, 'SELECT * FROM race  LIMIT 8', SQLITE_ASSOC);
foreach ($result as $entry) {
  echo 'Horse: ' . $entry['horse_num'] .' ' . $entry['horse_name'].' :- '.'number of bets : '. $entry['bets'] .':- '.' Payout :'. $entry['payout'].'<br>';
  $tbets = $tbets + $entry['bets'];
  
} 
echo "Total bets taken so far " . $tbets . "<br>";
$cresult=sqlite_query($db,"SELECT payout from race Where horse_num = 9");
 $res = sqlite_fetch_single($cresult)  ;
 $TCF = round($res/10);
 $TCFP = $TCF * 10 ;
 echo 'total cash for payouts :'.$TCFP ;
 $chresult=sqlite_query($db,"SELECT decimal_odds from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
echo '<br>'.'charity money raised though betting'.':-'.$cm   ;
$chresult=sqlite_query($db,"SELECT bets from race Where horse_num = 9");
$cm = sqlite_fetch_single($chresult) ;
 echo '<br>'.'Race Number'.':-'.$cm   ;
 sqlite_close($db);
 echo <<<endHTML

 
<form name="chatForm" method="POST" action="$PHP_SELF">
<input type="submit" value="1 bet on horse #1" name="b1"> 
<input type="submit" value="1 bet on horse #2" name="b2">  
<input type="submit" value="1 bet on horse #3" name="b3">  
<input type="submit" value="1 bet on horse #4" name="b4">   <br>
<input type="submit" value="1 bet on horse #5" name="b5"> 
<input type="submit" value="1 bet on horse #6" name="b6">  
<input type="submit" value="1 bet on horse #7" name="b7">  
<input type="submit" value="1 bet on horse #8" name="b8">   <br>

</form> 
 
 Change race 
<form name="change" method= "post" action="races.php">

select last race winner
Horse number: <select name="horse_num">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select><br>
 select next Race number: <select name="Race">

<option value="2">race2</option>
<option value="3">race3</option>
<option value="4">race4</option>
<option value="5">race5</option>
<option value="6">race6</option>
<option value="7">race7</option>
<option value="8">race8</option>
<option value="9">race9</option>
<option value="10">race10</option>
</select><br><br>
<p>
<input type="submit">
</p>
</form>
<a href="race1.php"> click here to clear all data base and load race 1 information</a>

</center>
</body>
</html>
 
endHTML;
#exit;


?>